Thread: Threads

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    Threads

    How do I use it?
    Does anyone can give me an example or some links?
    Thanks [:
    gavra.

  2. #2
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    What is your OS?

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It is fairly easy to google it
    For linux google for pthreads. If you have a more specific question ask, don't have any specific link in mind...

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    If its for linux then here is one of many links

    http://www.yolinux.com/TUTORIALS/Lin...ixThreads.html

  5. #5
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    For windows \:
    gavra.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    google for "Windows Multithreading" gives 2.4 million hits, and the first five looks like very useful.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Ok.
    I didn't understand the use of the parameter of thread and the parameters of the function that calls the thread (_beginthread), and why do I need to save the value that _beginthreads return?
    gavra.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    Ok.
    I didn't understand the use of the parameter of thread and the parameters of the function that calls the thread (_beginthread),
    Huh?
    In http://msdn.microsoft.com/en-us/libr...cb(VS.80).aspx
    there is no "thread" parameter, so I can't explain that. I suppose you mean the "startaddress" parameter - that should be the function that contains what your thread does.

    Stacksize tells what size the stack is for that function.

    arglist is passed unaltered to the function specified as startaddress.

    and why do I need to save the value that _beginthreads return?
    You don't HAVE to save that value - you need to check the value to see if you could create the thread successfully. I'm not sure if there's any function that takes the handle as a parameter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Look here:
    http://www.codeproject.com/KB/thread...threading.aspx
    I got confused @_@

    To make it more simple I'll ask: "how do I create a new thread and how do I use this thread?"
    gavra.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In the link above, this line creates a thread.
    Code:
           handle = (HANDLE) _beginthread( ThreadProc,0,&val); // create thread
    It implies that the returned value from _beginthread is actually a proper thread handle, and they use that to wait for the thread to finish.

    "using" a thread is simply just a matter of putting the right stuff in the thread-function. How you go about splitting a task into threads is a complicated subject, and without limiting the scope to something particular, it would take many many days to cover (there are probably dozens of PhD dissertations written on that subject alone).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    ohh I don't understand this stuff (sorry)
    I thought "_beginthread" just begin the thread and not creating.. @_@
    gavra.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Threads can be considered tasks.
    You create a task, then you execute it, pause it and resume it, or stop it.
    To perform these tasks, you need to tell the one who is scheduling the tasks (Windows) what task you want to change something for. To do this, you pass the task's "handle." A handle is a number (from our point of view) that tells the one who schedules the threads what task we're talking about.

    This translates exactly to what threads are about. The basics, anyway.
    _beginthread returns that number or identifier which you need to identify that specific thread.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Multi-threading is not a simple task, nor a simple program architecture to work with. Perhaps, learn C and programming in depth before attempting such a feat.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    ohh I don't understand this stuff (sorry)
    I thought "_beginthread" just begin the thread and not creating.. @_@
    That is just the name of the function, it actually both CREATES and STARTS a thread, where the work done by this thread is defined by the function given as "startaddress". By "creating" I personally mean creating the context (or environment) in which the thread executes, which include the OS represantion of a thread ('thread control block' or similar), stack (which is used to store parameters/arguments and return address when the thread is called and further calls to other functions within that thread). "Starting" means making the thread active to be able to run. In Windows (and most other multi-threaded environments) a thread can be created without actually starting it, but it's commonly that you actually want it to "start as soon as possible", so generally it is created and immediately set to "let it run" state.

    As discussed in the other posts here, multi-threading is definitely not a simple subject, so if you are not fully familiar single-threaded programming, going multithreaded is not going to help - it adds a whole new dimension to what can and will happen in your program.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    I am traying to do something very simple..
    no pausing and no resuming.
    Why is that so hard?!
    I'll try this ........ later, you may add simple examples..
    thanks anyway.
    gavra.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM