Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6594409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:48:26+00:00 2026-05-25T17:48:26+00:00

I am very surprised by the strange exception, that I got. class Threads {

  • 0

I am very surprised by the strange exception, that I got.

class Threads { 
    public:
        Threads() {}
        ~Threads() {}
        void StartThread(int (*p)()); //pointer to a function 
    private: 
        HANDLE hThread; 
        DWORD dwThreadID; 
    }; 

Method StartThread should receive pointer to my function (that will be run in another thread).
This function is simple. (as you can see it is situated outside the class Threads):

int MyThread() 
{
return 0; 
}

And this is method of creating thread:

inline void Threads::StartThread(int (*p)()) 
{
    hThread = CreateThread(NULL, 
            0, 
            (LPTHREAD_START_ROUTINE)(*p)(), 
            NULL,  
            0, 
            &dwThreadID); 

   if (hThread == NULL) 
        {
            return;
        }
}

Here compiler get error: cannot convert parameter 3 from ‘int’ to ‘LPTHREAD_START_ROUTINE’. That why I did the casting.

In main function I create object of type Threads and I try to call method StartThread. As parameter I send pointer to the function MyThread.

Threads *thread1; 
thread1 = new Threads(); 
thread1->StartThread(MyThread);

I thought MyThread must start in another thread. But the function MyTread always runs in Main Thread!!! And only after MyThread ends, another thread starts and then I get this exception:
Unhandled exception at 0x00000000 in ThreadClass.exe: 0xC0000005: Access violation.

I need clever advice!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:48:26+00:00Added an answer on May 25, 2026 at 5:48 pm

    It looks like you are actually calling the function on this line…

    (LPTHREAD_START_ROUTINE)(*p)()
    

    …and it returns an int that you’re casting. That just can’t work. How about:

    (LPTHREAD_START_ROUTINE)p 
    

    …instead?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very simply put: I have a class that consists mostly of static public members,
I am pretty new to silverlight and was very surprised to see that only
I'm very surprised that it seems impossible to upload more than a few megabytes
I am serializing a generic dictionary in VB.net and I am very surprised that
I use TMemo to display long strings. I was very surprised that method :
I'm very surprised to find that the following compiles: #include <iostream> using namespace std;
I was very surprised to get this error today, as it's one that I've
I was very surprised to get this error today, as it's one that I've
I am trying to debug a very strange class error by looking at the
I was very surprised when I found that a <div> with a size of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.