Thread: multi threaded program

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    28

    multi threaded program

    First off, how can I have a program running two different pieces of code simultaneously?

    Secondly, how could I (if possible) tell the program to run the two different pieces of code on different processors or cores if more than one core exists?

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I don't know about messing around with cores but for multithreading you want to use CreateThread().

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    best_answer->subject();
    think about that peice of code for a moment and it will give you a decent answer for your first question, as far as the second no idea

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Dash_Riprock
    First off, how can I have a program running two different pieces of code simultaneously?
    The means of doing this are system dependent. Generally, the notion is to use a thread of execution that runs a particular function. The means of creating a thread vary between systems. For example, CreateThread() under the win32 API, under posix the function to create a thread is pthread_create(). Under most versions of unix, processes are actually fairly lightweight, so it may be better to fork() a copy of the current process, and have the child process run different code [the return value from fork() can be used to detect if the current process is the parent or the child].
    Quote Originally Posted by Dash_Riprock
    Secondly, how could I (if possible) tell the program to run the two different pieces of code on different processors or cores if more than one core exists?
    Under unix I'm not sure on this one; I've never had to do it.

    With the win32 API, use GetSystemInfo() to determine the number of processors on a system, and either SetThreadAffinityMask() [to set processors that a thread is allowed to run on] or SetThreadIdealProcessor() [to specify the preferred processor for a thread]. There is also a function SetProcessAffinityMask() to set processors that a process is allowed to run on.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Heres a Multithreading tutorial
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I would also suggest the Boost.Thread library which offers a exception-safe portable solution.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    Alright, Ill try it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Multi file program?
    By zolo44 in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2002, 04:29 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM