GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Jan-2007, 11:50
qingxing2005 qingxing2005 is offline
New Member
 
Join Date: Jan 2007
Posts: 5
qingxing2005 is on a distinguished road
Smile

How to resume thread at the specific time instance?


Dear all,

I met one problem about resuming the thread. The thread function init() should be executed when the thread test_thread() is resumed. However, it is never done by my following snippet of code as follows. I don't know where the problem is. Can you help me a little?

Thanks in advance,

Bests,

L.M

Code:
CPP / C++ / C Code:
// Include relevant libs.
#include "windows.h"
...

//global vars.
HANDLE Handle_1 = 0;

DWORD WINAPI init()
{
	...
}

void test_thread(void)
{
	DWORD dwThreadId;
	
	Handle_1 = CreateThread( NULL, 0, init, NULL, CREATE_SUSPENDED, &dwThreadId );
												
}

int main() 
{
     ...
     test_thread();
     ResumeThread( Handle_1 );
     ...
}
Last edited by LuciWiz : 20-Jan-2007 at 14:03. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 20-Jan-2007, 13:11
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: How to resume thread at the specific time instance?


Are you sure? I compiled the below code with gcc (EDIT: converted from C++ to C, since this is the C forum) and ran it on Windows XP SP2:
CPP / C++ / C Code:
#include <stdio.h>
#include <windows.h>

DWORD WINAPI init(void *param)
{
    char *str = (char*)param;
    printf("7. thread received string: %s\n", str);
    return 0;
}

int main()
{
    HANDLE thrd = NULL;
    char *str = "qingxing2005";
    printf("1. creating thread...\n");
    thrd = CreateThread(NULL, 0, init, str, CREATE_SUSPENDED, NULL);
    if (thrd == NULL)
    {
        printf("failed to create thread!\n");
        return 1;
    }
    else
    {
        printf("2. created thread\n3. sent string: %s\n4. resuming thread...\n", str);
        if (ResumeThread(thrd) == -1)
        {
            printf("failed to resume thread!\n");
            return 1;
        }
        else
        {
            printf("5. resumed thread\n6. waiting for thread to finish...\n");
            WaitForSingleObject(thrd, INFINITE);
            printf("8. thread ended\n");
        }
    }
    getchar();
    return 0;
}
and got this output:
Code:
1. creating thread... 2. created thread 3. sent string: qingxing2005 4. resuming thread... 5. resumed thread 6. waiting for thread to finish... 7. thread received string: qingxing2005 8. thread ended
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
constructors/classes mapes479 C++ Forum 3 19-Nov-2006 17:34
Reading from a stream for a specific time a3.charles C Programming Language 7 27-Jul-2006 06:00
Thread itch C++ Forum 7 10-May-2006 15:10
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.