Help with simple multithreading.

This is a discussion on Help with simple multithreading. within the C++ Programming forums, part of the General Programming Boards category; (Borland c++ free command line compiler, WinXP) I'm just trying to multithread something simple, I attempted to make a multithreaded ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Help with simple multithreading.

    (Borland c++ free command line compiler, WinXP)
    I'm just trying to multithread something simple, I attempted to make a multithreaded version. Sence the multithreaded part of my code is short, but my actual code is long, I'll boil it down.

    Code:
    #include <windows.h>
    #include <iostream>
    #include <process.h>
    using namespace std;
    
    CRITICAL_SECTION CSector;
    
    int Gravity(int rate) {
     int y, x;
    
     for(y=0; y <= rate; y++) {
      EnterCriticalSection(&CSector);
      cout << "Gravity";
      LeaveCriticalSection(&CSector);
     }
    
     return 1;
    }
    
    int Move(int radius) {
     int y, x;
    
     for(y=0; y <= radius; y++) {
      EnterCriticalSection(&CSector);
      cout << "Move";
      LeaveCriticalSection(&CSector);
     }
    
     return 1;
    }
    
    int main() {
     HANDLE h[1];
    
     InitializeCriticalSection(&CSector);
     h[0] = (HANDLE)_beginthread(Gravity, 0, NULL);
     h[1] = (HANDLE)_beginthread(Move, 0, NULL);
    
     WaitForMultipleObjects(2, h, TRUE, INFINITE); 
    
     DeleteCriticalSection(&CSector);
     return 0;
    }
    But I get:
    Code:
    Error E2268 C:\Threadtest.cpp 36: Call to undefined function '_beginthread' in function main()
    *** 1 errors in Compile ***
    ?? I dont get why this is giving me an error -,-.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    might not be the reason for your error but for an array of 2 handles you'll need HANDLE h[2];

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Really? Whys that?

    I changed my code to reflect that, but it didint help. (h[2])
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Blackroot
    Really? Whys that?
    When defining an array the number in the square brackets is the number of elements. An array is zero based so the elements are numbered 0 thru number of elements-1.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    You basically need to add the name of the library to the command line as well.
    You need to look up the precise syntax for your compiler.

    Something like specify a library
    bcc32 prog.c thread.lib

    Or maybe specify multi-thread support throughout
    bcc32 -mt prog.c

  6. #6
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    neither exists for bcc32.
    Don't have full documentation here, have only the free compiler installed at work (to play around with, we're not a C++ shop), C++ Builder at home, so can't look into it further.

  7. #7
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Code:
    C:\Borland\BCC55\Bin>BCC32 C:\Borland\BCC5\Include\Process.h C:\ThreadTest.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    C:\Borland\BCC55\Include\process.h:
    C:\Threadtest.cpp:
    Error E2268 C:\Threadtest.cpp 36: Call to undefined function '_beginthread' in function main()
    *** 1 errors in Compile ***
    Doesnt work, the compiler doesnt understand -mt, I cant find anything containing the word "thread" or "multi" (not relating to multithreading anyways.) I opened process.h library, but I couldent understand how it worked. So I'm back to square one, why is this not compiling? When I use Ilink32 to link to process.h, I get:

    Code:
    C:\Borland\BCC55\Bin>ILINK32 -L C:\Borland\BCC55\Include\Process.h
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    Error: 'C:\BORLAND\BCC55\INCLUDE\PROCESS.H' contains invalid OMF record, type 0x
    2f
    I dont get this at all...
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  8. #8
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    You need to add
    Code:
    #define __MT__
    before including process.h.
    But that yields an unresolved external being generated by the linker, so there's something else missing as well.

    You'll also need to change your method signatures, as they're supposed to be void __cdecl functions that take a single void pointer as an argument. That's a mandatory signature.

    Start with something like
    Code:
    void __cdecl Gravity(void* rate) {
     int y, x;
    
     for(y=0; y <= 10; y++) {
      EnterCriticalSection(&CSector);
      cout << "Gravity";
      LeaveCriticalSection(&CSector);
     }
    }
    
    void __cdecl Move(void* radius) {
     int y, x;
    
     for(y=0; y <= 10; y++) {
      EnterCriticalSection(&CSector);
      cout << "Move";
      LeaveCriticalSection(&CSector);
     }

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    >I opened process.h library, but I couldent understand how it worked. So I'm back to square one, why is this not compiling? When I use Ilink32 to link to process.h
    Noticed you often use the term library when you actually talk about headers. Totally different things.
    Libraries contain compiled code.
    Headers just contain the declarations of the functions in the library.
    Keep in mind a linker does not know what a header is.
    A compiler has no idea about a library.
    Even as you sometimes use the same executable to compile and link a program it is still two different programs that do the job. The compiler ( and preprocesor ) generating binary code (object-files ) and the linker putting ia all together.
    There is actually a third program involved that creates libraries from object-files. Libraries are just collections of object-files.
    Kurt

  10. #10
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Code:
    #include <windows.h>
    #include <iostream>
    #include <process.h>
    using namespace std;
    
    #define __MT__
    
    CRITICAL_SECTION CSector;
    
    int Gravity(void* rate) {
     int y, x;
    
     x = 1;
    
     for(y=0; y <= x; y++) {
      EnterCriticalSection(&CSector);
      cout << "Gravity";
      LeaveCriticalSection(&CSector);
     }
    
     return 1;
    }
    
    int Move(void* radius) {
     int y, x;
    
     x=1;
     for(y=0; y <= x; y++) {
      EnterCriticalSection(&CSector);
      cout << "Move";
      LeaveCriticalSection(&CSector);
     }
    
     return 1;
    }
    
    int main() {
     HANDLE h[2];
    
     InitializeCriticalSection(&CSector);
     h[0] = (HANDLE)_beginthread(Gravity, 0, NULL);
     h[1] = (HANDLE)_beginthread(Move, 0, NULL);
    
     WaitForMultipleObjects(2, h, TRUE, INFINITE); 
    
     DeleteCriticalSection(&CSector);
     return 0;
    }
    Doesnt work, still gives me an error. Why would it take an arguement of a null pointer anyways? I'm still lost :/. I dont get why my program refuses to register process.h header when its their in the library file...?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  11. #11
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    You need to define __MT__ BEFORE including process.h, or it will not include the function.
    And you're not defining a nullpointer as the function argument, but a void pointer. That's just so you can pass it anything at all, at the penalty of having to provide your own type checking on the function argument.

  12. #12
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ok, I did everything and more, now it compiles. But when it triest ot create the .exe, it gives me this:

    Code:
    C:\Borland\BCC55\Bin>BCC32 C:\Threadtest.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    C:\Threadtest.cpp:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    Error: Unresolved external '__beginthread' referenced from C:\BORLAND\BCC55\BIN\
    THREADTEST.OBJ
    And my code is now:

    Code:
    #define __MT__
    #include <windows.h>
    #include <iostream>
    #include <process.h>
    using namespace std;
    
    CRITICAL_SECTION CSector;
    
    void Gravity(void *) {
     int y, x;
    
     x = 1;
    
     for(y=0; y <= x; y++) {
      EnterCriticalSection(&CSector);
      cout << "Gravity";
      LeaveCriticalSection(&CSector);
     }
    }
    
    void Move(void *) {
     int y, x;
    
     x=1;
     for(y=0; y <= x; y++) {
      EnterCriticalSection(&CSector);
      cout << "Move";
      LeaveCriticalSection(&CSector);
     }
    }
    
    int main() {
     HANDLE h[2];
    
     InitializeCriticalSection(&CSector);
     h[0] = (HANDLE)_beginthread(Gravity, 0, NULL);
     h[1] = (HANDLE)_beginthread(Move, 0, NULL);
    
     WaitForMultipleObjects(2, h, TRUE, INFINITE); 
    
     DeleteCriticalSection(&CSector);
     return 0;
    }
    I really hate thoes .obj errors... How do I fix this?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  13. #13
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Hmmm, I cant seem to get this to work... What are my alternatives to process.h multithreading? (Hopefully with a tutorial or example?) Or is their something obvious with this attempt I'm doing wrong?
    Last edited by Blackroot; 01-30-2006 at 10:19 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  14. #14
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ah nevermind, I found I could easily produce a substitute multithread. I just needed to rewrite my code (alot). Well, I'm going to keep trying to find why this is happening, but for now, I'm content :P.

    Well, thank you guys for trying!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  15. #15
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    For BCB and BC5 multithreading is a project option, so I guess it engages some linker flag somewhere.
    Maybe they include a lib which isn't included in the free version. Will have to compare when I get home.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21