Thread: A question about _beginthreadex(), a Win32 API Function

  1. #1
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35

    A question about _beginthreadex(), a Win32 API Function

    I'm confusing how to pass a argument into the thread function.After studying some documents about the thread, i wrote a program which could run correctly about the argument passing.But i feel that the code contains some potential dangers, for i must change the pointer point to the argument to void*, whick will be changed back in the thread function.
    Do you have any better and safer ways to pass the arguments?

    the source code:
    Code:
    /*
    传的是指针,对于某结构,先将其指针转化为void*, 再在线程函数的内部转化回来
    */
    
    #include <windows.h>
    #include <process.h>    /* _beginthread, _endthread */
    //#include <stddef.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <iostream>
    using std::cout;
    using std::endl;
    
    bool repeat = true;
    
    unsigned __stdcall test(void* pArguments) {
    	int* array = (int*)pArguments;
    	while (repeat) {
    		cout << array[1] << endl;
    		Sleep(500);
    	}
    	_endthreadex(0);
    	return 0;
    }
    
    unsigned __stdcall check(void* pArguments) {
    	getch();
    	repeat = false;
    	_endthreadex(0);
    	return 0;
    }
    
    int main() {
    	unsigned threadID[2];
    	
    	int array[] = {45, 34, 57};
    	HANDLE checkHandle = (HANDLE)_beginthreadex(NULL, 0, &check, NULL, 0, &threadID[0]);
    	HANDLE textHandle = (HANDLE)_beginthreadex(NULL, 0, &test, (void*)(array), 0, &threadID[1]);
    	WaitForSingleObject(textHandle, INFINITE);
    	CloseHandle(checkHandle);
    	CloseHandle(textHandle);
    	return 0;
    }
    Last edited by zouyu1983; 12-01-2006 at 11:02 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, there isn't. The casting is a sad fact about C-style generic APIs.

    But you could look into Boost.Thread, which hides it from you.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    Quote Originally Posted by CornedBee
    No, there isn't. The casting is a sad fact about C-style generic APIs.

    But you could look into Boost.Thread, which hides it from you.
    Sorry, but I don't quite understand what the "Boost.Thread" means?
    Hello, guys, i come from china, so i am not good at english. If you find the sentence i wrote full of mistakes, please tell me.
    I confirm that my english and programming skills will be improved with your help in this forum. thanks^_^

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It's part of the boost library: http://www.boost.org/doc/html/threads.html
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM