Nightmare with C++ multithreading

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 668 Views - Last Post: 06 December 2017 - 03:04 PM Rate Topic: ***-- 2 Votes

#16 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

Re: Nightmare with C++ multithreading

Posted 06 December 2017 - 02:12 AM

Ah, forgot the error message. It was "GameServer::StartListenThread must return a value" on line 77 which corresponds to the CreateThread line.

As ListenThread is an infinite loop unless an error occurs or listening stops, there's no value to return, so I returned 0 from both methods, but the unresolved externals then appear. I'm not at my home PC so I'll have to edit in the error messages later.

This post has been edited by Rixterz: 06 December 2017 - 02:15 AM

Was This Post Helpful? 0
  • +
  • -

#17 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,462
  • Joined: 05-May 12

Re: Nightmare with C++ multithreading

Posted 06 December 2017 - 04:58 AM

And so the error is quite correct in that you are not returning a value:
072 DWORD WINAPI GameServer::StartListenThread(void* s)
073 {
074     InstanceSocketStruct* st = (InstanceSocketStruct*)s;
075     st->instance->ListenThread(st->socket);
076 }
077



Even if your function has a logical infinite loop, the compiler still needs you to supply a return value for a function if you declare the function as returning a value.
Was This Post Helpful? 0
  • +
  • -

#18 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

Re: Nightmare with C++ multithreading

Posted 06 December 2017 - 05:24 AM

Do I even need to have it specified to return a value? This is based on an example I found which simply starts a thread and calls a method, so is that possible with a void method and I wouldn't have to return anything?
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,462
  • Joined: 05-May 12

Re: Nightmare with C++ multithreading

Posted 06 December 2017 - 09:22 AM

Your ListenThread() member function can be void, but not the StartListenThread() class function because you have to complY with the function signature. Not complying with this and doing an evil cast will screw up the callstack and cause all sorts of weird had to reproduce/diagnose problems.
Was This Post Helpful? 0
  • +
  • -

#20 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

Re: Nightmare with C++ multithreading

Posted 06 December 2017 - 02:32 PM

I've now returned 0 from StartListenThread and I've changed ListenThread to a void. Now for the ugly errors:

Error	LNK2019	unresolved external symbol [email protected] referenced in function "public: __thiscall GameServer::GameServer(void)" ([email protected]@[email protected])
Error	LNK1120	9 unresolved externals	
Error	LNK2019	unresolved external symbol [email protected] referenced in function "public: __thiscall GameServer::GameServer(void)" (??0Game[email protected]@[email protected])
Error	LNK2019	unresolved external symbol [email protected] referenced in function "public: __thiscall GameServer::GameServer(void)" ([email protected]@[email protected])
Error	LNK2019	unresolved external symbol [email protected] referenced in function "public: __thiscall GameServer::GameServer(void)" ([email protected]@[email protected])
Error	LNK2019	unresolved external symbol [email protected] referenced in function "public: __thiscall GameServer::GameServer(void)" ([email protected]@[email protected])
Error	LNK2019	unresolved external symbol [email protected] referenced in function "public: __thiscall GameServer::GameServer(void)" ([email protected]@[email protected])
Error	LNK2019	unresolved external symbol [email protected] referenced in function "private: void __thiscall GameServer::ListenThread(void *)" ([email protected]@@[email protected])
Error	LNK2019	unresolved external symbol [email protected] referenced in function "public: __thiscall GameServer::GameServer(void)" ([email protected]@[email protected])
Error	LNK2019	unresolved external symbol [email protected] referenced in function "public: __thiscall GameServer::GameServer(void)" ([email protected]@[email protected])




EDIT: Solved by adding #pragma comment(lib, "WS2_32")

This post has been edited by Rixterz: 06 December 2017 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

#21 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,462
  • Joined: 05-May 12

Re: Nightmare with C++ multithreading

Posted 06 December 2017 - 02:47 PM

Good job!
Was This Post Helpful? 0
  • +
  • -

#22 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

Re: Nightmare with C++ multithreading

Posted 06 December 2017 - 03:04 PM

Awesome. Thanks very much for the help, usually all I get from people is negativity.

I'm bound to post more threads in the future but for now I have enough of an understanding to be able to debug some things myself and hopefully produce a decent amount of code before the next issue arises :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2